
module Mil
  module Object
    # This class provides a simple abstaction of a key/value pair. It 
    # provides syntax behavior similar as a Hash object, except it
    # can have only one key and corrosponding value.
    class KeyValue
      attr_accessor :key
      attr_accessor :value

      def initialize(options = {})
	unless options.empty?
	  if options.keys.size <= 2 and [ :key, :value ].is_subset? options.keys
	    @key = options[:key]
	    @value = options[:value]
	  else
	    raise ArgumentError, "invalid option(s) passed."
	  end
	end
      end

      # Key/Value Assignment - assigns the key and its corrosponding value.
      def []=(key, value)
	@key = key
	@value = value
      end

      # Key/Value Reference - retrieves the value for the key. Raises an error
      # if the key provides is not the currently set key.
      def [](key)
	if key == @key
	  @value
	else
	  raise ArgumentError, "#{key} is not the current key."
	end
      end
    end
  end
end
